package com.hailong.zyt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hailong.zyt.beans.CourseAll;
import com.hailong.zyt.beans.CourseUncheck;
import com.hailong.zyt.mapper.CourseAllMapper;
import com.hailong.zyt.mapper.CourseMapper;
import com.hailong.zyt.mapper.CourseUncheckMapper;
import com.hailong.zyt.mapper.StudentMapper;
import com.hailong.zyt.service.CourseUncheckService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hailong
 * @since 2021-06-08
 */
@Service
public class CourseUncheckServiceImpl extends ServiceImpl<CourseUncheckMapper, CourseUncheck> implements CourseUncheckService {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private CourseAllMapper courseAllMapper;

    @Override
    public IPage<CourseUncheck> getRequestList(Long teaId, Integer pn) {
        QueryWrapper<CourseUncheck> wrapper = new QueryWrapper<>();
        wrapper.eq("tea_id", teaId);

        Page<CourseUncheck> courseUncheckPage = new Page<>(pn, 8);
        IPage<CourseUncheck> pages = this.page(courseUncheckPage, wrapper);
        for(CourseUncheck courseUncheck : pages.getRecords()) {
            courseUncheck.setStudent(studentMapper.selectById(courseUncheck.getStuId()));
            courseUncheck.setCourse(courseMapper.selectById(courseUncheck.getCouId()));
        }
        return pages;
    }

    @Override
    public void acceptChosen(Long couId, Long teaId, Long stuId) {
        Map<String, Object> map = new HashMap<>();
        map.put("cou_id", couId);
        map.put("tea_id", teaId);
        map.put("stu_id", stuId);
        CourseAll courseAll = new CourseAll();
        courseAll.setCouId(couId);
        courseAll.setTeaId(teaId);
        courseAll.setStuId(stuId);
        if(courseAllMapper.insert(courseAll) > 0){
            this.removeByMap(map);
        }
    }

    @Override
    public void refuseChosen(Long couId, Long teaId, Long stuId) {
        Map<String, Object> map = new HashMap<>();
        map.put("cou_id", couId);
        map.put("tea_id", teaId);
        map.put("stu_id", stuId);
        this.removeByMap(map);
    }
}
